package com.foolishbird.sdk.ui.assist;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.foolishbird.sdk.params.bean.GameNoticeModel;
import com.foolishbird.sdk.utils.ResUtils;

import java.util.ArrayList;
import java.util.List;

public class GameNoticeListAdapter extends BaseAdapter {

    private final List<GameNoticeModel> mChannels = new ArrayList<>();
    private LayoutInflater inflater;
    private int lastSelectedIndex;
    private int UIType;

    public void setAdapterData(List<GameNoticeModel> data) {
        this.mChannels.clear();
        this.mChannels.addAll(data);
        notifyDataSetChanged();
    }

     public GameNoticeListAdapter(int type){
         UIType = type;
    }

    public void setLastSelectedIndex(int position) {
        this.lastSelectedIndex = position;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mChannels.size();
    }

    @Override
    public GameNoticeModel getItem(int position) {
        return mChannels.get(position);
    }

    public GameNoticeModel getSelectItem(){
        return  mChannels.get(lastSelectedIndex);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Context context = parent.getContext();
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();

            if (inflater == null) {
                inflater = LayoutInflater.from(context);
            }

            final int layoutResId = ResUtils.getLayoutIdentifier(context, "hnssn_item_gamenotice");
            final int titleResId = ResUtils.getViewIdentifier(context, "hnssn_gamenotice_title");

            convertView = inflater.inflate(layoutResId, parent, false);
            viewHolder.account = convertView.findViewById(titleResId);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        GameNoticeModel channel = getItem(position);
        if (channel != null) {
//            viewHolder.account.setText(channel.getTitleSubtitle());
            if(UIType==1) {
                viewHolder.account.setTextColor(position == lastSelectedIndex ?
                        Color.parseColor("#5f2904") :
                        Color.parseColor("#cc9900"));
                viewHolder.account.setBackgroundResource(position == lastSelectedIndex ?
                        ResUtils.getDrawableIdentifier(context, "hnssn_gamenotice_list_select") :
                        ResUtils.getDrawableIdentifier(context, "hnssn_gamenotice_list_nomal")
                );
            }else {
                viewHolder.account.setTextColor(Color.parseColor("#cc6600"));
                viewHolder.account.setBackgroundResource(position == lastSelectedIndex ?
                        ResUtils.getDrawableIdentifier(context, "hnssn_gamenotice_list_select") :
                        ResUtils.getDrawableIdentifier(context, "hnssn_gamenotice_list_nomal_chuanqi"));
            }
        }
        return convertView;
    }

    public static class ViewHolder {
        public TextView account;
    }

}
